package ua.kharkov.gritsay.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.Proxy;

import javax.swing.JOptionPane;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class proxy {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		String str_url = new String("http://myip.by/");
		
		URL url = new URL(str_url); // java.net.URL
		
		String proxy_host = new String("129.105.110.46");
		
		int proxy_port = 1085;
		
		Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxy_host, proxy_port));
		
		HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
		//connection.setConnectTimeout(4000); // если нужен таймаут
		connection.connect(); // подключились. теперь получаем Document

		String line = null;
		StringBuffer buffer_page = new StringBuffer();
		BufferedReader buffer_input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
												
		while ((line = buffer_input.readLine()) != null) {
			buffer_page.append(line);
		}
		Document doc = Jsoup.parse(String.valueOf(buffer_page));

		System.out.println(doc);

		try {
			connection.disconnect();
		} catch (NullPointerException e) {
			JOptionPane.showMessageDialog(null, "Ошибка отключения! " + e);
		}
	}
}
